﻿using GetData;
using jutehui.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace jutehui.GetData
{
    public class ShiHuo
    {

        HtmlAgilityPack.HtmlDocument doc;
        public ShiHuo()
        {
            doc = new HtmlAgilityPack.HtmlDocument();
        }
        /// <summary>
        /// 通过网页html获取商品信息并保存到数据库中
        /// </summary>
        /// <param name="url"></param>
        /// <param name="shoppingType">海淘2 精选1</param>
        /// <returns></returns>
        public string GetProductsToDataBase(string url, int type)
        {
            //  string url = "http://www.shihuo.cn/";
            var htmlText = CustomMethod.GetHtmlByHttpHelper(url);
            string msg = "";
            var pList = GetProductByRegex(htmlText, type);
            if (pList == null)
            {
                return "";
            }
            DataTable dt = CustomMethod.ListToDataTable<Product>(pList, "Product", ref msg);
            if (dt == null || dt.Rows.Count < 1)
            {
                return "";
            }
            jutehui.BLL.Product bll = new jutehui.BLL.Product();
            //string msg = "";
            if (bll.SaveTableData("Product", dt, out msg))
            {
                return dt.Rows.Count.ToString(); ;
            }
            else
            {
                msg = "识货：GetProductsToDataBase" + msg;
                CustomMethod.WriteLog(msg);
                return "";

            }
        }
        /// <summary>
        /// 根据正则解析商品信息
        /// </summary>
        /// <param name="htmlText"></param>
        /// <param name="type">海购还是精选</param>
        /// <returns></returns>
        public List<Product> GetProductByRegex(string htmlText, int type)
        {
            List<Product> pList = new List<Product>(); 
            doc.LoadHtml(htmlText);
            var collec = doc.DocumentNode.SelectNodes("//div[@class='list-v1']");
            var list = new List<string>();
            if (collec == null || collec.Count < 1)
            {
                return null;
            }
            for (int index = 0; index < collec.Count; index++)
            {
                list.Add(collec[index].InnerHtml);
            }

            //每个商品匹配所有相关属性并赋值
            for (int index = 0; index < list.Count; index++)
            {
                Product product = new Product();
                try
                {
                  
                    product = GetProduct(list[index]); 
                    if(product==null)
                    {
                        continue;
                    }
                    product.UpdateTimeStr = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    product.UpdateTime = DateTime.Now;
                    product.SoldCount = 0;
                    product.Pname = product.Pname = System.Web.HttpUtility.HtmlDecode(product.Pname);// product.Pname.Replace("&#x2F;", "/").Replace("#039;", "'").Replace("&amp;", "&").Replace("&#x27;", "'").Replace("&quot;", "\""); ;
                    
                }
                catch (Exception ex)
                {
                    string msg = "识货网：GetProductByRegex ，" + ex.Message.ToString();
                    CustomMethod.WriteLog(msg);
                    continue;
                }
                pList.Add(product);
            }
            var newList = CustomMethod.GetDistinctProdcuts(pList);
            newList.Reverse();
            return newList;
        }
        /// <summary>
        ///根据规则获取属性值 一个商品item
        /// </summary>
        /// <param name="htmlContent"></param>
        /// <returns></returns>
        public Product GetProduct(string htmlContent)
        {
            Product pro = new Product();
            pro.FromWeb = "识货";
            pro.TypeId = 1;
            pro.ProductTypeId = 16;
            doc.LoadHtml(htmlContent);
            //名称
           var namelist= doc.DocumentNode.SelectNodes("//div[@class='title']");
           if (namelist != null)
           {
               pro.Pname = namelist[0].InnerText.Replace("&nbsp;","");
           }
           
            //图片
           var picL = CustomMethod.GetAttr(doc, htmlContent, "//a[@class='title']/img", "src");
           if (picL != null)
           {
               pro.ImgUrl = picL[0];
           }
            //价格
           var priL = CustomMethod.GetValue(doc, "//div[@class='title']/a[@class='title']/s");
           if (!string.IsNullOrEmpty(priL))
           {
               pro.Price = priL.Replace("&nbsp;","");
           }
            //数据源
           var sourL = CustomMethod.GetValue(doc, "//div[@class='zd-link']/span");
           if (!string.IsNullOrEmpty(sourL))
           {
               pro.Source = sourL;
           }
            //链接 
           var urL = CustomMethod.GetAttr(doc, htmlContent, "//div[@class='zd-link']/a", "href");
           if (urL != null)
           {
               string dirUrl = urL[0];
               string productUrl = "";
               string trueUrl = CustomMethod.GetTrueUrl(dirUrl, ref productUrl);
               pro.Memo = productUrl;
               pro.Plink = trueUrl;
               if (pro.Plink == "")
               {   
                   if (productUrl == "")
                   {
                       CustomMethod.WriteLog("识货网：没有抓取到商品链接");
                   }
                   return null;
               }
           }

           var detailurl = CustomMethod.GetAttr(doc, htmlContent, "//div[@class='title']/a", "href");
            //详细
           if (detailurl != null && detailurl.Count>0)
           {
               string conthtml = CustomMethod.GetHtmlByHttpHelper(detailurl[0]);
               doc.LoadHtml(conthtml);
               var desList = doc.DocumentNode.SelectNodes("//div[@class='article']/div/p");
               string desStr = "";
               if(desList!=null)
               {
                   for(int k=0;k<desList.Count;k++)
                   {
                       if (desList[k].InnerText == "")
                       {
                           continue;
                       }
                       desStr += "<p>"+desList[k].InnerText+"</p>";
                   }
                   pro.Description = desStr;
               }
               else
               {
                   pro.Description = pro.Pname; 
               }
               //详细图
               var despics = CustomMethod.GetAttr(doc, conthtml, "//div[@class='con clearfix editor-frame frame']/p/a/img", "src");
               if(despics!=null)
               {
                   if (despics.Count ==1)
                   {
                       pro.ImgUrl2 = despics[0]; 
                   }
                    if(despics.Count==2)
                    {
                        pro.ImgUrl2 = despics[0];
                        pro.ImgUrl3 = despics[1];
                    }
                    else if (despics.Count >= 3)
                    {
                        pro.ImgUrl2 = despics[0];
                        pro.ImgUrl3 = despics[1];
                        pro.ImgUrl4 = despics[2];
                    }
               }
           }
           else
           {
               return null;
           }
            return pro;
        }
    }
}
